<template>
    <div class="user-setting">
        <basic-container>
            <div class="user-setting__main">
                <div class="user-setting__title">安全设置</div>
                <div class="user-setting__item">
                    <div class="user-setting__meta">
                        <p class="title">
                            <i class="el-icon-user"></i>
                            账号安全
                        </p>
                        <p class="subtitle">更改当前账号密码</p>
                    </div>
                    <div class="user-setting__menu">
                        <el-button size="small" @click="changePassword">修改</el-button>
                    </div>
                </div>
            </div>
            <!--<el-tabs tab-position="left"
                     stretch
                     style="height: 400px;">
              <el-tab-pane label="安全设置">
                <div class="user-setting__main">
                  <div class="user-setting__title">安全设置</div>
                  <div class="user-setting__item">
                    <div class="user-setting__meta">
                      <p class="title">
                        <i class="el-icon-user"></i>
                        账号安全
                      </p>
                      <p class="subtitle">更改当前账号密码</p>
                    </div>
                    <div class="user-setting__menu">
                      <el-button size="small">修改</el-button>
                    </div>
                  </div>
                  <div class="user-setting__item">
                    <div class="user-setting__meta">
                      <p class="title">
                        <i class="el-icon-mobile-phone"></i>
                        绑定手机
                      </p>
                      <p class="subtitle">设置您的绑定手机号码</p>
                    </div>
                    <div class="user-setting__menu">
                      <el-button size="small">修改</el-button>
                    </div>
                  </div>
                  <div class="user-setting__item">
                    <div class="user-setting__meta">
                      <p class="title">
                        <i class="el-icon-message"></i>
                        绑定邮箱
                      </p>
                      <p class="subtitle">设置您的绑定邮箱</p>
                    </div>
                    <div class="user-setting__menu">
                      <el-button size="small">修改</el-button>
                    </div>
                  </div>
                </div>

              </el-tab-pane>
              <el-tab-pane label="通知设置">
                <div class="user-setting__main">
                  <div class="user-setting__title">通知设置</div>
                  <div class="user-setting__item">
                    <div class="user-setting__meta">
                      <p class="title">
                        <i class="el-icon-document"></i>
                        系统消息
                      </p>
                      <p class="subtitle">系统性的通知或者更新消息</p>
                    </div>
                    <div class="user-setting__menu">
                      <el-switch v-model="value">
                      </el-switch>
                    </div>
                  </div>
                  <div class="user-setting__item">
                    <div class="user-setting__meta">
                      <p class="title">
                        <i class="el-icon-user"></i>
                        帐号消息
                      </p>
                      <p class="subtitle">帐号变更的通知消息</p>
                    </div>
                    <div class="user-setting__menu">
                      <el-switch v-model="value">
                      </el-switch>
                    </div>
                  </div>
                </div>
              </el-tab-pane>
            </el-tabs>-->
            <el-dialog
                    title="更改当前账号密码"
                    :visible.sync="passwordDialogBox"
                    append-to-body
                    width="40%"
                    :before-close="passwordDialogClose">
                <avue-form ref="passwordForm" v-model="passwordForm" :option="passwordOption" @submit="submitPassword">
                    <!--<template slot="menuForm">
                        <el-button type="primary" @click="submitPassword">提 交</el-button>
                        <el-button @click="passwordDialogClose">取 消</el-button>
                    </template>-->
                </avue-form>
                <!--<span slot="footer" class="dialog-footer">
                    <el-button @click="passwordDialog = false">取 消</el-button>
                    <el-button type="primary" @click="passwordDialog = false">确 定</el-button>
                 </span>-->
            </el-dialog>
        </basic-container>
    </div>
</template>

<script>
    import store from '@/store/';
    import router from '@/router/router'
    import {changePassword} from '@/api/user'

    export default {
        data() {
            return {
                value: true,
                passwordDialogBox: false,
                passwordForm: {},
                passwordOption: {
                    emptyBtn: false,
                    submitBtn: true,
                    labelWidth: 100,
                    column: [
                        {
                            label: "原密码",
                            prop: "password",
                            type: 'password',
                            span: 24,
                            maxlength: 20,
                            showWordLimit: true,
                            rules: [
                                {
                                    required: true,
                                    message: "请输入原密码",
                                    trigger: "blur"
                                }
                            ]
                        },
                        {
                            label: "新密码",
                            prop: "newPassword",
                            type: 'password',
                            span: 24,
                            maxlength: 20,
                            showWordLimit: true,
                            rules: [
                                {
                                    required: true,
                                    message: "请输入新密码",
                                    trigger: "blur"
                                },
                                {
                                    min: 4,
                                    max: 20,
                                    message: '长度在 4 到 20 个字符',
                                    trigger: 'blur'
                                }
                            ]
                        },
                        {
                            label: "确认新密码",
                            prop: "confirmNewPassword",
                            type: 'password',
                            span: 24,
                            maxlength: 20,
                            showWordLimit: true,
                            rules: [
                                {
                                    required: true,
                                    message: "请输入确认新密码",
                                    trigger: "blur"
                                },
                                {
                                    min: 4,
                                    max: 20,
                                    message: '长度在 4 到 20 个字符',
                                    trigger: 'blur'
                                },
                                {
                                    validator: this.validPassword,
                                    trigger: 'blur'
                                }
                            ]
                        },
                    ]
                }
            }
        },
        methods: {
            validPassword(rule, value, callback) {
                if (value !== this.passwordForm.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else if (value === this.passwordForm.password) {
                    callback(new Error('新密码不能与原密码相同'));
                } else {
                    callback();
                }
            },
            passwordDialogClose(done) {
                this.$refs.passwordForm.resetForm();
                done();
            },
            changePassword() {
                this.passwordDialogBox = true;
            },
            submitPassword(form, done) {
                this.$refs.passwordForm.validate((vaild) => {
                    if (vaild) {
                        changePassword(form).then(() => {
                            this.$message.success('操作成功,请重新登录');

                            setTimeout(() => {
                                store.dispatch('FedLogOut').then(() => router.push({path: '/login'}));
                                done();
                            }, 2500)
                        }).catch(() => {
                            done();
                        })

                    }
                });

            }
        }
    }
</script>

<style lang="scss">
    .user-setting {
        &__main {
            padding: 8px 40px;
        }
        &__item {
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center;
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
            padding: 12px 0;
            border-bottom: 1px solid #eee;
        }
        &__title {
            font-size: 20px;
            color: rgba(0, 0, 0, 0.85);
            line-height: 28px;
            font-weight: 500;
            margin-bottom: 12px;
        }
        &__meta {
            flex: 1;
            .title {
                margin: 6px 0;
                font-weight: 700;
                font-size: 14px;
            }
            .subtitle {
                margin: 8px 0;
                font-size: 14px;
                color: #555;
            }
        }
    }
</style>
